from tkinter import * 
from PIL import Image, ImageTk

class Tk_grid:
    #初始化pack
    def __init__(self):
        #初始化
        self.tk = Tk()
        self.tk.title('自动机')
        #self.tk.geometry('800x600')
    def loadPic1(self,path):
        self.pilImage1 = Image.open(path)
        self.pilImage1 = self.resize(200,300,self.pilImage1)
        self.tkImage1 = ImageTk.PhotoImage(image=self.pilImage1)
        return self.tkImage1
    def loadPic2(self,path):
        self.pilImage2 = Image.open(path)
        self.pilImage2 = self.resize(200,300,self.pilImage2)
        self.tkImage2 = ImageTk.PhotoImage(image=self.pilImage2)
        return self.tkImage2
    #载入图片
    def loadF1(self):
        self.label1 = Label(self.tk, text='匹配图')
        self.label1.grid(row=0,column=0)
        self.label1 = Label(self.tk, image=self.tkImage1,fg="green",bd=10,width=300,height=400)
        self.label1.image = self.tkImage1
        self.label1.grid(row=1,column=0,sticky=W)
    def loadF2(self):
        self.label1 = Label(self.tk, text='手机截图')
        self.label1.grid(row=0,column=2)
        self.label2 = Label(self.tk, image=self.tkImage2,bg="pink",width=300,height=400)
        self.label2.grid(row=1,column=2,sticky=E)
    #缩放图片
    def resize(self,w_box, h_box, pil_image): #参数是：要适应的窗口宽、高、Image.open后的图片
      w, h = pil_image.size #获取图像的原始大小   
      f1 = 1.0*w_box/w 
      f2 = 1.0*h_box/h    
      factor = min([f1, f2])   
      width = int(w*factor)    
      height = int(h*factor)    
      return pil_image.resize((width, height), Image.ANTIALIAS)
    def mainloop(self):
        self.tk.mainloop()

# if __name__ == '__main__':
    # PATH1 = "G:/python/img/tmp.png"
    # PATH2 = "G:/python/tldk/1/home.png"
    # app = Tk_grid()
    # app.loadPic1(PATH1)
    # app.loadF1()
    # app.loadPic2(PATH2)
    # app.loadF2()
    # app.mainloop()
    